package test05;

import java.sql.*;

public class t2_1 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        //1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        //2.获取连接url,用户名,密码
        String url = "jdbc:mysql://localhost:3306/t1?characterEncoding=UTF-8";
        Connection con = DriverManager.getConnection(url, "root", "123456");
        //System.out.println(con);

        //3.获取Statement对象
        Statement statement = con.createStatement();

        //4.获取 PrepareStatement预编译对象
        String sql = "select * from employee where name = ? and gender = ? ";
        PreparedStatement ps = con.prepareStatement(sql);
        
        //设置占位符参数
//        ps.setString(1,"张三");
//        ps.setString(2,"男");

        ps.setString(1,"李冰冰");
        ps.setString(2,"女");

        //5.执行查询处理结果集
        ResultSet resultSet = ps.executeQuery();

        while(resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String gender = resultSet.getString("gender");
            double salary = resultSet.getDouble("salary");
            double bonus = resultSet.getDouble("bonus");
            Date join_date = resultSet.getDate("join_date");
            System.out.println(id + "," + name + "," + gender + "," + salary + "," + bonus + "," + join_date);
        }

        //关闭连接
        resultSet.close();
        statement.close();
        con.close();
    }
}
